package com.sinosoft.call.service;

import java.util.Iterator;
import java.util.Map.Entry;

import com.sinosoft.utils.logback.LogUtil;
import com.sinosoft.utils.xml.XmlUtil;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.DocumentException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sinosoft.call.entity.model.CallConfig;


public interface CallService {
	public static final String _YES="1";
	public static final String _NO="0";
	/**URL请求超时标识 **/
	public static final String TIMEOUT = "TIMEOUT";
	/**无数据 标识 **/
	public static final String NODATA = "NODATA";
	/**请求失败 标识**/
	public static final String RQUESTERRO = "RQUESTERRO";
	/**
	 * @param callConfig 配置
	 * @param param 入参
	 * @return {Success:状态,Message:信息,Data:当状态成功时，请求返回报文的JSON字串}
	 * @author zouren
	 */
	public JSONObject callService(CallConfig callConfig,JSON param);
	
	/**
	 * 返回报文统一处理 结果
	 * 
	 * @param responseStr
	 *            返回报文
	 * @param serviceCode   对应WsdlConfig表中的code
	 * @param printflag   对应WsdlConfig表中的printflag(1 不打印响应报文) 
	 *         
	 * @return 结果 {Success:true为成功，false为失败,Message:提示信息}
	 * @author zouren
	 */
	default JSONObject getFormatResponseXML(String responseStr,
			String serviceCode,String printflag) {
		JSONObject result = null;
//		LogUtil.business("serviceCode:{},响应报文::{}", serviceCode,responseStr);
		String logMassage;
		
		if (StringUtils.isBlank(responseStr)) {
			result = pubResultJSON(false, "暂无数据!", null);
			logMassage = "返回空,serviceCode:" + serviceCode;
		} else if (TIMEOUT.equals(responseStr)) {
			result = pubResultJSON(false, "系统请求超时!", null);
			logMassage = "超时标识 ,serviceCode:" + serviceCode;
		} else if (NODATA.equals(responseStr)) {
			result = pubResultJSON(false, "系统无数据!", null);
			logMassage = "无数据,serviceCode:" + serviceCode;
		} else if (RQUESTERRO.equals(responseStr)) {
			result = pubResultJSON(false, "系统请求失败!", null);
			logMassage = "请求失败,serviceCode:" + serviceCode;
		} else {
			// 避免结果解析异常
			responseStr = responseStr.replaceAll(
					"<!DOCTYPE TCRMService SYSTEM  \"tCRMResponse.dtd\">", "");
			// 处理BO返回错误
			responseStr = responseStr.replaceAll("><!--", ">");
			responseStr = responseStr.replaceAll("--><", "<").trim();
			JSON jso = null;
			if (responseStr.startsWith("{")) {// JSON对象字串
				jso = JSONObject.parseObject(responseStr);
			} else {// XML
				try {
					jso = XmlUtil.documentToJSONObject(responseStr);
				} catch (DocumentException e) {
					e.printStackTrace();
				}
			}
			result = pubResultJSON(true, "获取数据成功", jso);
			logMassage = jso.toString();
		}
		if("1".equals(printflag)){
			LogUtil.business("响应报文不打印 ,serviceCode:{}", serviceCode);
		}else{
//			LogUtil.business("serviceCode:{}  响应报文:{}", serviceCode,logMassage);
		}
		
		return result;
	}
	/**公用组装JOSN
	 * @param result 状态
	 * @param info 信息
	 * @param data 数据
	 * @return {Success:状态,Message:信息,Data:数据}
	 * @author zouren
	 */
	default JSONObject pubResultJSON(boolean result,String info,Object data){
		JSONObject json = new JSONObject();
		json.put("Success", result);
		json.put("Message",info==null?"":info);
		json.put("Data", data==null?"":data);
		return json;
	}
	/**
	 * @param massageTemplet 信息模板
	 * @param map 替换的参数据  使用 map中的#+key+# 来定位massageTemplet文件的位置  并使用value替换
	 * @return 替换后的信息
	 * @author zouren
	 */
	default  String getMassage(String massageTemplet,JSONObject map){
		String re ="";
		if(massageTemplet!=null&&map!=null&&map.size()>0){
			re = massageTemplet;
			Iterator<Entry<String, Object>> it =map.entrySet().iterator();
			while(it.hasNext()){
				Entry<String, Object> row= it.next();
				re = re.replace("#"+row.getKey()+"#", row.getValue()!=null?row.getValue().toString(): "");
			}
		}
		return re;
	}
}
